import requests


# 异步协程  是单线程
import asyncio
import aiohttp
url = 'https://api.web.360kan.com/v1/rank?cat=1'


async def fetch(session, url):
    async with session.get(url) as response:
       return  await response.json()


async def main():
    async with aiohttp.ClientSession() as session:
            urls = [f'https://api.web.360kan.com/v1/rank?cat={i}' for i in range(1,7) ]
            f = [fetch(session,url) for url in urls]
            values = await asyncio.gather(*f)
            for v in values:
                print(v["data"],type(v["data"]))


asyncio.run(main())



# 多线程
import threading

def t_main(url):
    res = requests.get(url)
    print(res.json(),threading.current_thread().name)
def main():
    urls = [f'https://api.web.360kan.com/v1/rank?cat={i}' for i in range(1,7)]
    list = []
    for url in urls:
        # 创建线程
        t = threading.Thread(target=t_main, name =f"子线程{url}" ,args=(url,))
        # 开始线程
        t.start()
        # 将线程都添加到列表中
        list.append(t)
    # 循环这个列表阻塞住线程的  要让子线程先结束了再结束主线程
    for t in list:
        t.join()


if __name__ == '__main__':
    main()




